package com.hskj.springboot.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String Hello123(){
        System.out.println("Hello! security");
        //登录用户数据获取
        //从SecurityContextHolder获取Context在内容中获取完整的认证信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //从完整的认证信息里面获取身份信息
        Object principal = authentication.getPrincipal();
        System.out.println("principal = " + principal);
        //从完整的认证信息里面获取权限信息
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        System.out.println("authorities = " + authorities);
        return "Hello! security";
    }
    @RequestMapping("/hello1")
    public String Hello1231(){
        System.out.println("Hello! security");
        //多线程登录用户数据获取
        new Thread(()->{
            //从SecurityContextHolder获取Context在内容中获取完整的认证信息
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            //从完整的认证信息里面获取身份信息
            Object principal = authentication.getPrincipal();
            System.out.println("principal = " + principal);
            //从完整的认证信息里面获取权限信息
            Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
            System.out.println("authorities = " + authorities);
        }).start();
        return "Hello! security";
    }
}
